package com.finance.report.service;


import com.finance.base.constant.BaseConstants;
import com.finance.base.exception.BaseException;
import com.finance.report.dao.LogisticsDataMapper;
import com.finance.report.dao.LogisticsErrorLogMapper;
import com.finance.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import com.finance.report.bean.pojo.LogisticsData;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class LogisticsDataService implements ILogisticsDataService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Resource
    private LogisticsDataMapper logisticsDataMapper;

    @Resource
    private LogisticsErrorLogMapper logisticsErrorLogMapper;

    @Override
    @Deprecated
    public LogisticsData getByWaybillNum(Date finishDate, String waybillNum) {
        if(finishDate == null){
            this.logger.info("未知的时间参数 单号：{}",waybillNum);
            return null;
        }
        String tableName = this.getDataTableName(finishDate);
        return logisticsDataMapper.getByWaybillNum(tableName,waybillNum);
    }
    @Override
    @Cacheable(value = "logistics",cacheResolver = "tableNameCacheResolver",key = "#tableName+'_'+#waybillNum",unless="#result == null")
    public LogisticsData getByWaybillNum(String tableName, String waybillNum) {
        return logisticsDataMapper.getByWaybillNum(tableName,waybillNum);
    }


    @Override
    @CacheEvict(value = "logistics",cacheResolver = "logisticsDataCacheResolver", key = "#root.target.getDataTableName(#logisticsData.SignTime)+'_'+#logisticsData.waybillNum",condition="#result == true ")
    public boolean add(LogisticsData logisticsData) throws BaseException{
        String tableName = this.getDataTableName(logisticsData.getSignTime());
        try{
            return logisticsDataMapper.add(tableName,logisticsData) == 1;
        }catch (Exception e){
            if(e instanceof com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException|| e instanceof org.springframework.dao.DuplicateKeyException){
                this.logger.info("运单数据已存在 过滤{}",logisticsData.getWaybillNum());
                return true;
            }else{
                this.logger.info("运单数据插入失败{}",logisticsData.getWaybillNum());
                this.logger.error(e.getMessage(),e);
                return false;
            }

        }
    }
    @Override
    @Transactional
    @CacheEvict(value = "logistics",cacheResolver = "signDateCacheResolver",allEntries = true)
    public boolean deleteByMonthDate(Date finishDate) {
        String dataTableName = this.getDataTableName(finishDate);
        String errorTableName = this.getErrorTableName(finishDate);
        String extendTableName = this.getExtendTableName(finishDate);
        String month = DateUtil.dateFormat(finishDate,DateUtil.YEAR_AND_MONTH_FORMAT);
        return logisticsDataMapper.deleteByTableName(dataTableName) >= 0 &&
                logisticsDataMapper.deleteByTableName(extendTableName) >= 0 &&
                logisticsErrorLogMapper.deleteByMonth(errorTableName,month) >=0;

    }

    @Override
    @CachePut(value = "logistics",cacheResolver = "logisticsDataCacheResolver", key = "#root.target.getDataTableName(#logisticsData.SignTime)+'_'+#logisticsData.waybillNum",condition="#result != null")
    public LogisticsData update(LogisticsData logisticsData) throws BaseException {
        String tableName = this.getDataTableName(logisticsData.getSignTime());
        if(logisticsDataMapper.update(tableName,logisticsData) == 1) {
            return logisticsData;
        }
        return null;
    }

    @Override
    public List<LogisticsData> addressNameAcceptdayPostman(String addressConsignee, String nameConsignee, String postmanNo, Date acceptTime, String tableName) {
        return this.logisticsDataMapper.addressNameAcceptdayPostman(addressConsignee,nameConsignee,postmanNo,acceptTime,tableName);
    }

    @Override
    public List<LogisticsData> addressNameSignday3Meta(String addressConsignee, String nameConsignee, Date signDay, String tableName) {
        return this.logisticsDataMapper.addressNameSignday3Meta(addressConsignee,nameConsignee,signDay,tableName);
    }

    @Override
    public List<LogisticsData> getByBusinessCode(String businessCode, String tableName) {
        return this.logisticsDataMapper.getByBusinessCode(businessCode,tableName);
    }

    @Override
    public List<LogisticsData> waybillNumSuffix(String waybillNum, String tableName) {
        return this.logisticsDataMapper.waybillNumSuffix(waybillNum,tableName);
    }

    /**
     * 获取操作表名
     * @return
     */
    @Override
    public String getDataTableName(Date signTime){
        return BaseConstants.SYSCONFIG_KEY_LOGISTICDATA_TABLE+ DateUtil.dateFormat(signTime,"yyMM");
    }
    /**
     * 获取操作表名
     * @return
     */
    @Override
    public String getDataTableName(String yyMM){
        return BaseConstants.SYSCONFIG_KEY_LOGISTICDATA_TABLE+ yyMM;
    }

    /**
     * 获取操作表名
     * @return
     */
    public String getExtendTableName(Date signTime){
        return BaseConstants.SYSCONFIG_KEY_LOGISTICS_EXTEND_TABLE + DateUtil.dateFormat(signTime,"yyMM");
    }

    /**
     * 获取操作表名
     * @return
     */
    public String getErrorTableName(Date signTime){
        return BaseConstants.SYSCONFIG_KEY_LOGISTICERROR_TABLE + DateUtil.dateFormat(signTime,"yyMM");
    }
}
